import { useNavigate } from "react-router-dom";
/** 获取用户身份 */
export const useIdentity = () => {
  const navigate = useNavigate();
  const identity = window.localStorage.getItem('DLC_IDENTITY');
  const username = window.localStorage.getItem('DLC_USERNAME');
  const onUnLogin = () => {
    window.localStorage.removeItem('DLC_IDENTITY');
    window.localStorage.removeItem('DLC_USERNAME');
    navigate('/');
  }
  return [identity, username, onUnLogin];
}

export const useLogin = () => {
  const navigate = useNavigate();
  return (
    userInfo: {
      username: string;
      identity: string;
  }) => {
    if(userInfo?.identity && userInfo?.username) {
      window.localStorage.setItem('DLC_IDENTITY', userInfo.identity);
      window.localStorage.setItem('DLC_USERNAME', userInfo.username);
      navigate('/');
    }
  }
}